package com.pro_one.service.impl;

import com.pro_one.dao.OrderSettingDao;
import com.pro_one.pojo.OrderSetting;
import com.pro_one.service.OrderSettingService;
import com.pro_one.util.DateUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional(readOnly = true)
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 文件上传后添加预约设置信息，如果当日已有预约信息，则修改，否则新增
     *
     * @param orderSettings
     */
    @Override
    @Transactional(readOnly = false)
    public void add(List<OrderSetting> orderSettings) {
        if (orderSettings != null && orderSettings.size() > 0) {
            for (OrderSetting orderSetting : orderSettings) {
                this.editOrAdd(orderSetting);
            }
        }
    }

    /**
     * 根据月份查询当月预约设置信息
     *
     * @param date
     * @return
     */
    @Override
    public List<Map> findOrdersettingByMonth(String date) {
        Calendar calendar = Calendar.getInstance();
        String[] moreDate = date.split("-");

        //动态获取每月的最后一天
        calendar.set(Integer.parseInt(moreDate[0]), Integer.parseInt(moreDate[1]) - 1, 1);
        calendar.roll(Calendar.DATE, -1);
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String firstDay = date + "-1";
        String lastDay = format.format(calendar.getTime());
        Map map = new HashMap();
        map.put("firstDay", firstDay);
        map.put("lastDay", lastDay);
        List<OrderSetting> orderSettings = orderSettingDao.findOrdersettingByMonth(map);
        List<Map> allOrdersettings = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettings) {
            Map<String, Object> info = new HashMap<>();
            info.put("date", orderSetting.getOrderDate().getDate());
            info.put("number", orderSetting.getNumber());
            info.put("reservations", orderSetting.getReservations());
            allOrdersettings.add(info);
        }
        return allOrdersettings;
    }

    /**
     * 修改预约设置信息
     *
     * @param orderSetting
     */
    @Override
    @Transactional(readOnly = false)
    public void edit(OrderSetting orderSetting) {
        this.editOrAdd(orderSetting);
    }

    /**
     * 根据预约日期查询预约设置信息
     *
     * @param orderDate
     * @return
     * @throws Exception
     */
    @Override
    public OrderSetting findOrdersettingByOrderDate(String orderDate) throws Exception {
        return orderSettingDao.findByOrderdate(DateUtils.parseString2Date(orderDate));
    }

    public void editOrAdd(OrderSetting orderSetting) {
        int count = orderSettingDao.findCountByOrderdate(orderSetting.getOrderDate());
        if (count > 0) {
            orderSettingDao.editNumberByOrderDate(orderSetting);
        } else {
            orderSettingDao.add(orderSetting);
        }
    }
}
